<?php

namespace App\Controllers\Order;

use App\Models\Order\Order;
use \App\Libs\Pay\PayLibs;
use \App\Libs\Helper\WeixinHelper;

class AlipayPayOrder extends \App\Libs\Base\BaseAction
{

    protected function beforeRun()
    {
        parent::beforeRun();
        $this->needLogin = TRUE;
    }

    //此接口仅仅是获取调动手机APP的，没有修改订单功能
    public function execute()
    {

        $uid = $this->session->user->id;
        if (!$this->request->has('order_sn')) {
            return \App\Libs\Views\Json::view(1, '参数不完整', NULL);
        }
		 if (!$this->request->has('pay_type')) {
            return \App\Libs\Views\Json::view(1, '参数不完整', NULL);
        }
        $orderSn = $this->request->get('order_sn');
        $payType = $this->request->get('pay_type');//支付方式 1.微信app 2.支付宝app
        //检查订单是否可以支付
        $OrderOrCode = Order::checkOrder($uid, $orderSn);
        if (is_numeric($OrderOrCode)) {
            switch ($OrderOrCode) {
                case -10:
                    return \App\Libs\Views\Json::view(1, '支付失败，未找到对应订单!', Null);
                    break;
                case -4:
                    return \App\Libs\Views\Json::view(1, '支付失败，订单已过期！', Null);
                    break;
                default:
                    return \App\Libs\Views\Json::view(1, '支付遇到问题,请查看订单状态!', Null);
                    break;
            }
        }
        //支付订单信息
        $goods = $OrderOrCode->getRelated('App\Models\Goods\Goods');
        $goodsInfo = [
            'subject' => '派乐熊:' . $goods->goods_name,
            'out_trade_no' => $OrderOrCode->order_sn,
            'total_amount' => $OrderOrCode->price,   //订单总金额
            'product_code' => 'QUICK_MSECURITY_PAY',   //固定值
            'passback_params' => urlencode(json_encode([
                'type' => 'buy'
            ]))
        ];
		
		//支付处理
		switch( $payType ){
			case 1:
				//$code = $this->request->get("code");
				$time = time();
				$code = null;
				//添加支付方式的标识;
				$OrderOrCode->pay_method = 1;  //1.微信 2.支付宝  3:派乐宝
				if (!$OrderOrCode->update()) {
					return \App\Libs\Views\Json::view(1, '失败，系统繁忙请稍后！', null);
				}
				$prePayId = WeixinHelper::prePay( $code , $goodsInfo['out_trade_no'] , $goodsInfo['subject'] , $goodsInfo['total_amount'] * 100);
				$nonceStr = \App\Libs\Captcha\Algorithms\Character::instance()->generate(mt_rand(20, 32))['captcha'];
				$payPullStr = array(
					'appid' => WeixinHelper::$appID,
					'partnerid' => WeixinHelper::$mchID,
					'timestamp' => $time,
					'noncestr' => $nonceStr,
					'prepayid' => $prePayId,
					'package' => 'Sign=WXPay'
				);
				ksort($payPullStr);
				$str = urldecode(http_build_query($payPullStr));
				$str .= '&key=' . WeixinHelper::$key;
				$payPullStr['sign'] = strtoupper(md5($str));
				break;
			case 2:
				//添加支付方式的标识;
				$OrderOrCode->pay_method = 2;  //1.微信 2.支付宝  3:派乐宝
				if (!$OrderOrCode->update()) {
					return \App\Libs\Views\Json::view(1, '失败，系统繁忙请稍后！', null);
				}
				$alipay = new PayLibs(1);
				$payPullStr = $alipay->getAlipayAppStr($goodsInfo);
				break;
			default:
				break;
		}
        
        return \App\Libs\Views\Json::view(0, '成功', $payPullStr);
    }

}
